package erreAga.dao.toplink;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import erreAga.dao.HoraExtraDao;
import erreAga.eb.HoraExtra;


public class HoraExtraDaoToplinkImpl implements HoraExtraDao {

  @Override
  public void deleteHoraExtra(HoraExtra horaExtra) {
    EntityManager manager = DatabaseManagerSingletonToplinkImpl.getInstancia().getManager();
    manager.getTransaction().begin();
    manager.remove(horaExtra);
    manager.getTransaction().commit();
  }

  @Override
  public HoraExtra getHoraExtra(Integer id) {
    EntityManager manager = DatabaseManagerSingletonToplinkImpl.getInstancia().getManager();
    return manager.find(HoraExtra.class, id);
  }

  @Override
  public HoraExtra saveHoraExtra(HoraExtra horaExtra) {
    EntityManager manager = DatabaseManagerSingletonToplinkImpl.getInstancia().getManager();
    manager.getTransaction().begin();
    manager.persist(horaExtra);
    manager.getTransaction().commit();
    return horaExtra;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<HoraExtra> searchHoraExtra() {
    EntityManager manager = DatabaseManagerSingletonToplinkImpl.getInstancia().getManager();
    Query query=manager.createQuery ("select a from HoraExtra a");
    List<HoraExtra> resultList = (List<HoraExtra>) query.getResultList();
    return resultList;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<HoraExtra> searchHoraExtra(String matriculaFuncionario) {
    EntityManager manager = DatabaseManagerSingletonToplinkImpl.getInstancia().getManager();
    Query query=manager.createQuery ("select a from HoraExtra a where a.funcionario.matricula = "+matriculaFuncionario);
    List<HoraExtra> resultList = (List<HoraExtra>) query.getResultList();
    return resultList;
  }

}
